import Line from "./Line"
import { fillArc, fillText } from "@/utils/common"

export default class Arc {

  constructor(x, y, index, color, left, right) {
    this.x = x
    this.y = y
    this.index = index
    this.color = color
    this.left = left
    this.right = right
  }

  setLeft(left) {
    this.left = left
  }

  setRight(right) {
    this.right = right
  }

  static R = 20

  draw(ctx) {
    ctx.beginPath()
    ctx.fillStyle = this.color

    fillArc(ctx, this.x, this.y, Arc.R, 0, Math.PI * 2)

    if (this.index !== undefined) {
      // 绘制下标
      ctx.fillStyle = 'red'
      fillText(ctx, this.index, this.x, this.y + Arc.R + 8)
      ctx.fillStyle = 'black'
    }

    // 绘制左线
    if (this.left) {
      this.left.draw(ctx)
    }

    // 绘制右线
    if (this.right) {
      this.right.draw(ctx)
    }
  }


  deepCopy() {
    let left = undefined
    let right = undefined
    if (this.left) {
      left = new Line(this.left.fromX, this.left.fromY, this.left.toX, this.left.toY)
    }

    if (this.right) {
      right = new Line(this.right.fromX, this.right.fromY, this.right.toX, this.right.toY)
    }

    return new Arc(this.x, this.y, this.index, this.color, left, right)
  }
}